//
//  UIView+Animation.swift
//  PublicPod
//
//  Created by 许俊华 on 2021/9/6.
//

import Foundation

public extension UIView {
    func aq_addRoaAnimation() -> Void {
        /*
            CATransform3DMakeRotation(M_PI, 0, 1, 0); 以 Y 轴动画
            CATransform3DMakeRotation(M_PI, 1, 0, 0); 以 X 轴动画
            CATransform3DMakeRotation(M_PI, 0, 0, 1); 以 Z 轴动画
         */
        UIView.animate(withDuration: 0.3) {
            self.layer.transform = CATransform3DMakeRotation(.pi, 0, 1, 0)
        }
        
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + (0.3 * Double(NSEC_PER_SEC))) {
            UIView.animate(withDuration: 0.7, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 0.2, options: .curveEaseOut, animations: {
                self.layer.transform = CATransform3DMakeRotation(2 * .pi, 0, 1, 0)
            }, completion: nil)
        }
    }
    
    func aq_addShakeAnimation() -> Void {
        
        let angle:CGFloat = .pi * 0.05
        
        self .aq_addShakeAnimation(repeatCount: 1, angle: angle)
    }
    
    func aq_addShakeAnimation(repeatCount:Float, angle:CGFloat) -> Void {
        //需要实现的帧动画，这里根据需求自定义
        self.layer.removeAnimation(forKey: "shake")
        
        let animation:CAKeyframeAnimation = CAKeyframeAnimation.init()
        animation.keyPath = "transform.rotation"
        
        animation.values = [-angle, angle, -angle]
        
        animation.repeatCount = repeatCount
        animation.calculationMode = .cubic
        
        self.layer.add(animation, forKey: "shake")
    }
    
    // MARK:左右抖动
    func aq_addLeftAndRightShakeAnimation() -> Void {
        self.layer.removeAnimation(forKey: "transform.rotation.z")
        
        let shake:CABasicAnimation = CABasicAnimation.init(keyPath: "transform.rotation.z")
        shake.duration = 0.1
        shake.autoreverses = true
        shake.repeatCount = 3
        shake.isRemovedOnCompletion = false
        shake.fromValue = -0.1;
        shake.toValue = 0.1
        self.layer.add(shake, forKey: "transform.rotation.z")
    }
    
    func aq_addScaleAnimation(scale:CGFloat) -> Void {
        
        self.layer.removeAnimation(forKey: "scale")
        
        let animation:CAKeyframeAnimation = CAKeyframeAnimation.init()
        animation.keyPath = "transform.scale"
        

        animation.values = [0.8, scale, 1.0, 0.8, scale, 1.0]
        animation.duration = 0.5
        animation.calculationMode = .cubic
        self.layer.add(animation, forKey: "scale")
    }
    
    func addRotationAnimation(repeatCount:Float, begainAngle:CGFloat, endAngel:CGFloat) -> Void {
        
        self.layer.removeAnimation(forKey: "rotation")
        
        let animation:CAKeyframeAnimation = CAKeyframeAnimation.init()
        
        animation.keyPath = "transform.rotation"
        

        animation.values = [begainAngle, endAngel]
        animation.repeatCount = repeatCount
        animation.speed = 1
        animation.duration = 1
        animation.calculationMode = .cubic
        self.layer.add(animation, forKey: "rotation")
    }
    
    // MARK:阴影
    func setShadow(sColor:UIColor,offset:CGSize,
                   opacity:Float,radius:CGFloat, cornerSize:CGFloat) {
        //设置阴影颜色
        self.layer.shadowColor = sColor.cgColor
        //设置透明度
        self.layer.shadowOpacity = opacity
        //设置阴影半径
        self.layer.shadowRadius = radius
        //设置阴影偏移量
        self.layer.shadowOffset = offset
        // 设置圆角
        self.layer.cornerRadius = cornerSize
    }
    
    /// MARK:部分圆角设置
      ///
      /// - Parameters:
      ///   - view: 需要设置的控件
      ///   - corner: 哪些圆角
      ///   - radii: 圆角半径
      /// - Returns: layer图层
      func configRectCorner(view: UIView, corner: UIRectCorner, radii: CGSize) -> Void {
          
          let maskPath = UIBezierPath.init(roundedRect: view.bounds, byRoundingCorners: corner, cornerRadii: radii)
          
          let maskLayer = CAShapeLayer.init()
          maskLayer.frame = view.bounds
          maskLayer.path = maskPath.cgPath
          
        self.layer.mask = maskLayer
      }
}
